
#include "config.h"
#ifdef CONFIG_WITH_ECLIPSE
#include "WProgram.h"
#endif
#ifdef CONFIG_WITH_ETHERNET
#include <Server.h>
#include <Client.h>
#include <Ethernet2.h>
#endif

#ifdef CONFIG_WITH_DHCP
#  include "Dhcp.h"
#endif
#ifdef CONFIG_WITH_TIMER
#  include "TimerOne.h"
#endif

#include "Led.h"
#include "XbLiteReader.h"
#include "XbEvent.h"


#ifdef CONFIG_WITH_ECLIPSE
extern "C" void __cxa_pure_virtual() {  }
#endif

XbLiteReader 	xbee 	= XbLiteReader();
XbEvent 	listener	= XbEvent();

#ifdef CONFIG_WITH_DHCP
Dhcp dhcp;
#endif

// flash rgbled with the power value
#ifdef CONFIG_WITH_TIMER
void on_timer(){

}
#endif



void setup(){
   // initialize the digital pin as an output:
   pinMode(CONFIG_LEDPULSE, OUTPUT);
   pinMode(CONFIG_LEDERROR, OUTPUT);
//   pinMode(CONFIG_LEDNONE, OUTPUT);
//   pinMode(CONFIG_LEDGND, OUTPUT);

  xbee.begin(9600);
#ifdef CONFIG_WITH_TIMER
   Timer1.initialize(1000000);
   Timer1.attachInterrupt(on_timer);
#endif

   // reboot/retry while dhcp is not available and blink error led
#ifdef CONFIG_WITH_DHCP
   Serial.println("dhcp");
   while(dhcp.beginWithDHCP(mac)!=1){
	   Led::flash(CONFIG_LEDERROR);
	   delay(5000);
   }

   // wait for init
   delay(5000);
#endif

   Led::flash(CONFIG_LEDERROR);
   Led::flash(CONFIG_LEDPULSE);
   
   Serial.println("init done...");
#ifdef CONFIG_WITH_DEBUG
#endif   
}


 // the loop() method runs over and over again,
 // as long as the Arduino has power

void loop(){
   xbee.read();
   if(xbee.error == NO_DATA){
	   return;
   }

   if (xbee.error) {
	   listener.onError(xbee);
	   return;
   }

   // got something
   if (xbee.app_id != SERIES1_IOPACKET) {
	   listener.onError(xbee);
	   return;
   }

   // got a rx packet
   listener.onReceiveData(xbee);
   delay(1);
}

